package com.fsc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fsc.entity.domain.PictureVideo;
import com.fsc.service.PictureVideoService;
import com.fsc.mapper.PictureVideoMapper;
import com.fsc.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
* @author 86183
* @description 针对表【picture_video(学生签到签退表)】的数据库操作Service实现
* @createDate 2024-06-02 13:15:22
*/
@Service
public class PictureVideoServiceImpl extends ServiceImpl<PictureVideoMapper, PictureVideo>
    implements PictureVideoService{

    private static final String endpoint = "oss-cn-guangzhou.aliyuncs.com";
    private static final String bucketName = "su-bucket666";
    @Autowired
    private PictureVideoMapper pictureVideoMapper;

    @Override
    public List<PictureVideo> getAllAttendance() {
        return pictureVideoMapper.getAllAttendance();
    }

    @Override
    public List<PictureVideo> getAttendanceByStudentId(int studentId) {
        return pictureVideoMapper.getAttendanceByStudentId(studentId);
    }

    @Override
    public List<PictureVideo> getAttendanceByTeacherId(int teacherId) {
        return pictureVideoMapper.getAttendanceByTeacherId(teacherId);
    }

    @Override
    public List<PictureVideo> getAttendanceByParentId(int parentId) {
        return pictureVideoMapper.getAttendanceByParentId(parentId);
    }
    @Override
    public String uploadImage(MultipartFile file, int studentId, Date time) throws IOException {
        // 生成文件名
        String fileName = generateUniqueFileName(file.getOriginalFilename());
        // 上传文件到OSS并获取URL
        OssUtil.uploadImage(file.getBytes(), fileName);
        // 格式化时间以匹配数据库中的时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedTime = sdf.format(time);

        // 更新图片URL到数据库
        int rowsUpdated = pictureVideoMapper.updatePictureVideoUrl(studentId, formattedTime, fileName);

        if (rowsUpdated > 0) {
            // 返回完整URL
            return "https://" + bucketName + "." + endpoint + "/" + fileName;
        } else {
            throw new IOException("Failed to update database with new image URL");
        }
    }

    private String generateUniqueFileName(String originalFilename) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return "image/" + uuid + originalFilename;
    }

    @Override
    public void addAttendance(MultipartFile file, int studentId, String status, Date time) throws IOException {
        String fileName = generateUniqueFileName(file.getOriginalFilename());
        OssUtil.uploadImage(file.getBytes(), fileName);
        pictureVideoMapper.insertAttendance(studentId, status, fileName, time);
    }

    @Override
    public void updateAttendance(int id, String status, Date time) {
        pictureVideoMapper.updateAttendance(id, status, time);
    }

    @Override
    public void deleteAttendance(int id) {
        pictureVideoMapper.deleteAttendance(id);
    }
}




